//
//  DQBQuestionCollectionViewCell.m
//  DrivingQuestionBank
//
//  Created by mac on 2023/12/28.
//

#import "DQBQuestionCollectionViewCell.h"

@implementation DQBQuestionCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self answerCellSubView];
    }
    return self;
}

- (void)setModel:(DQBQuestionModel *)model {
    _model = model;
    _questionTitle.text = _model.content;
    
    if(_model.image){
        [self.contentView addSubview:[self questionImageView]];
        [_questionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.width.mas_equalTo(ScreenWidth-40);
            make.height.mas_lessThanOrEqualTo(100);
            make.top.mas_equalTo(self.questionTitle.mas_bottom).offset(10);
            make.bottom.mas_equalTo(-10);
        }];
        _questionImageView.image = [UIImage imageNamed:_model.image];
    } else {
        [_questionTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-10);
        }];
    }
    
}

- (void)answerCellSubView{
    [self.contentView addSubview:[self questionTitle]];
    [_questionTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(ScreenWidth-40);
        make.height.mas_greaterThanOrEqualTo(20);
        make.top.mas_equalTo(10);
    }];
    
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    [self setNeedsLayout];
    [self layoutIfNeeded];
    return [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
}


#pragma mark -- lazy

- (UIImageView *)questionImageView{
    if (!_questionImageView) {
        _questionImageView = [[UIImageView alloc] init];
    }
    return _questionImageView;
}
- (UILabel *)questionTitle{
    if (!_questionTitle) {
        _questionTitle = [[UILabel alloc] init];
        _questionTitle.font = [UIFont boldSystemFontOfSize:17];
        _questionTitle.text = @"这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目这是一个题目";
        _questionTitle.numberOfLines = 0;
    }
    return _questionTitle;
}


@end
